package com.tangcco.lockwell.aop;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.base.SystemCode;
import com.tangcco.lockwell.domain.TopicNalysis;
import com.tangcco.lockwell.domain.dto.ai.AiDto;
import com.tangcco.lockwell.service.TopicNalysisService;
import com.tangcco.lockwell.utility.DateTimeUtil;
import com.tangcco.lockwell.utility.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 调用ai服务执行的切面·
 * @author yanxinping
 * @version 1.0
 * @description: TODO
 * @date 2024/12/27 11:06
 */
@Slf4j
@Aspect
@Component
public class AiPostExecuteAspect {
    @Autowired
    private TopicNalysisService topicNalysisService;

    @Around("@annotation(com.tangcco.lockwell.anno.AiPostExecute)")
    public Object checkDbBeforeExecute(ProceedingJoinPoint joinPoint) throws Exception {
        Object responseObj = null;
        try {
            log.info("进入ai服务执行切面");
            Object[] args = joinPoint.getArgs();
            if (args.length > 1 && args[1] instanceof AiDto) {
                AiDto aiDto = (AiDto)args[1];
                List<TopicNalysis> topicNalysises = topicNalysisService.selectForLongSentenceAnalysis(aiDto.getQuestionId(),aiDto.getParagraph());
                if(CollectionUtil.isNotEmpty(topicNalysises)){
                    TopicNalysis topicNalysis = topicNalysises.get(0);
                    if(topicNalysis.getAnalysis()!= null){
                        log.info("从数据库中获取到分析结果,直接返回");
                        JsonNode response = JsonUtil.toJsonObject(topicNalysis.getAnalysis(), JsonNode.class);
                        return RestResponse.ok(response);
                    }
                }
                //方法执行
                log.info("执行ai服务");
                responseObj = joinPoint.proceed();
                if(responseObj instanceof RestResponse){
                    //如果返回的是RestResponse，则直接返回
                    //将结果存入数据库
                    RestResponse restResponse = (RestResponse)responseObj;
                    if(restResponse.getCode() == 1 && restResponse.getResponse()!= null){
                        String analysis = String.valueOf(restResponse.getResponse());
                        TopicNalysis topicNalysis = new TopicNalysis();
                        topicNalysis.setParagraph(aiDto.getParagraph());
                        topicNalysis.setQuestionId(aiDto.getQuestionId());
                        topicNalysis.setSubjectMatter(aiDto.getText());
                        topicNalysis.setAnalysis(analysis);
                        topicNalysis.setStatus(2);
                        topicNalysis.setCreateTime(DateTimeUtil.dateFormat(new Date()));
                        topicNalysisService.insertByFilter(topicNalysis);
                    }
                    return responseObj;
                }
            }
        } catch (Throwable e){
            log.error(e.getMessage(), e);
//            throw new Exception(e.getMessage());
            return RestResponse.fail(SystemCode.InnerError.getCode(),e.getMessage());
        }
        return responseObj;
    }



}
